package com.frxuexi.sign

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.MyNavTo
import com.frxuexi.lib_base.NavPath
import com.frxuexi.lib_base.UiState
import com.frxuexi.sign.entity.SignEntity
import com.frxuexi.sign.intent.SignIntent
import com.frxuexi.sign.viewmodel.SignViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun SignPage(
    navHostController: NavHostController,
    vm: SignViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {
    var list = remember {
        SnapshotStateList<SignEntity>()
    }
    val kv = MMKV.defaultMMKV()

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess -> {
                        Log.i("===", it.data.toString())
                        list.addAll(it.data as Collection<SignEntity>)
                    }

                    is UiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(SignIntent.getSignList(mapOf("userId" to 1, "page" to 1)))
    })
    Scaffold(bottomBar = {
        var singState by remember {
            mutableStateOf("签到")
        }
        var isEnable by remember {
            mutableStateOf(true)
        }

        Button(
            onClick = {
                val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                val nowTime = format.format(System.currentTimeMillis())
                if (singState == "签到") {
                    //保存签到状态和当前时间
                    kv.encode("sign", "sign,$nowTime")
                    //按钮变为可签退
                    singState = "签退"
                } else {
                    //保存签退状态和当前时间
                    kv.encode("sign", "sout,$nowTime")
                    //按钮变为不可用
                    isEnable = false
                }
//                MyNavTo.list.add(NavPath.SIGN_COMMIT)
//                navHostController.navigate(NavPath.SIGN_COMMIT)
                      MyNavTo.navigateTo(navHostController,NavPath.SIGN_COMMIT)
            },
            Modifier
                .fillMaxWidth()
                .padding(horizontal = 10.dp), enabled = isEnable
        ) {
            //获取当前日期
            val nowDate = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
            //sign/sout,2024-07-30 12:12:12
            val sign = kv.decodeString("sign", "")
            if (sign.isNullOrEmpty()) {
                singState = "签到"
            } else {
                val split = sign.split(",")
                //判断目前是签到状态还是签退状态
                val state = split[0]
                //签到、签退的时间
                val time = split[1]
                //说明今天签到或者签退过
                if (time.substring(0, 10) == nowDate) {
                    if (state == "sign") {//说明已经签到过
                        singState = "签退"
                    } else {//说明已经签退过
                        isEnable = false
                    }
                } else {//说明今天还没签过
                    singState = "签到"
                }
            }

            Text(text = singState)


        }
    }) {
        Column(Modifier.padding(it)) {
            BaseTitle(navHostController = navHostController, title = "考勤")
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp), horizontalArrangement = Arrangement.SpaceBetween
            ) {
                val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
                Text(text = "时间$week")
                Text(text = "考勤规则")
            }
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list) {
                    SignItem(it)
                }
            })
        }
    }


}

@Composable
fun SignItem(entity: SignEntity) {
    val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(entity.signTime)
    Text(
        text = time,
        Modifier
            .fillMaxWidth()
            .padding(horizontal = 20.dp)
            .background(
                colorResource(id = R.color.lightblue)
            )
            .padding(horizontal = 10.dp, vertical = 20.dp)
    )
}